# when should cudaThreadSynchronize() be called?

**URL:** <https://forums.developer.nvidia.com/t/when-should-cudathreadsynchronize-be-called/19357>\
**Category:** CUDA Programming and Performance\
**Created:** [October 22, 2010, 8:05am UTC](https://forums.developer.nvidia.com/t/when-should-cudathreadsynchronize-be-called/19357 "2010-10-22T08:05:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![London](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@London](https://forums.developer.nvidia.com/u/London)\
**Post date:** [October 22, 2010, 8:05am UTC](https://forums.developer.nvidia.com/t/when-should-cudathreadsynchronize-be-called/19357/1 "2010-10-22T08:05:55Z")

</div>

Hi, when should cudaThreadSynchronize() be called and what is this function doing?

In the code below, there are three cudaThreadSynchronize() calls. Which of them are redundant? Which of them are necessary? Does not the kernel functions call cudaThreadSynchronize() automatically?

```auto
for(int i=0; i<NumberOfSimualtion; i++)

{

	for(int j=0; j<NumberOfSteps; j++)

	{

		 kernel1<<<60,32>>>(a,b,c);

		 cudaThreadSynchronize(); 

		 kernel2<<<60,32>>>(d,e,f);

		  cudaThreadSynchronize(); 

	 }

}

cudaThreadSynchronize(); 

printf("Done");

```

Thanks

---

<div class="post-metadata">

**Author:** ![London](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@London](https://forums.developer.nvidia.com/u/London)\
**Post date:** [October 22, 2010, 8:05am UTC](https://forums.developer.nvidia.com/t/when-should-cudathreadsynchronize-be-called/19357/2 "2010-10-22T08:05:55Z")

</div>

Hi, when should cudaThreadSynchronize() be called and what is this function doing?

In the code below, there are three cudaThreadSynchronize() calls. Which of them are redundant? Which of them are necessary? Does not the kernel functions call cudaThreadSynchronize() automatically?

```auto
for(int i=0; i<NumberOfSimualtion; i++)

{

	for(int j=0; j<NumberOfSteps; j++)

	{

		 kernel1<<<60,32>>>(a,b,c);

		 cudaThreadSynchronize(); 

		 kernel2<<<60,32>>>(d,e,f);

		  cudaThreadSynchronize(); 

	 }

}

cudaThreadSynchronize(); 

printf("Done");

```

Thanks

---

<div class="post-metadata">

**Author:** ![tera](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tera](https://forums.developer.nvidia.com/u/tera)\
**Post date:** [October 22, 2010, 8:40am UTC](https://forums.developer.nvidia.com/t/when-should-cudathreadsynchronize-be-called/19357/3 "2010-10-22T08:40:34Z")

</div>

The first two are redundant, as kernels in the same stream are launched sequentially anyway, and no code is executed on the CPU between the kernels.

The third call does serve a purpose, without it the “Done” message would appear before execution is actually completed.

---

<div class="post-metadata">

**Author:** ![tera](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tera](https://forums.developer.nvidia.com/u/tera)\
**Post date:** [October 22, 2010, 8:40am UTC](https://forums.developer.nvidia.com/t/when-should-cudathreadsynchronize-be-called/19357/4 "2010-10-22T08:40:34Z")

</div>

The first two are redundant, as kernels in the same stream are launched sequentially anyway, and no code is executed on the CPU between the kernels.

The third call does serve a purpose, without it the “Done” message would appear before execution is actually completed.

---

<div class="post-metadata">

**Author:** ![London](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@London](https://forums.developer.nvidia.com/u/London)\
**Post date:** [October 22, 2010, 9:40am UTC](https://forums.developer.nvidia.com/t/when-should-cudathreadsynchronize-be-called/19357/5 "2010-10-22T09:40:33Z")

</div>

> [@](#):
>
> The first two are redundant, as kernels in the same stream are launched sequentially anyway, and no code is executed on the CPU between the kernels.
> 
> The third call does serve a purpose, without it the “Done” message would appear before execution is actually completed.

Thanks. Very helpful.

---

<div class="post-metadata">

**Author:** ![London](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@London](https://forums.developer.nvidia.com/u/London)\
**Post date:** [October 22, 2010, 9:40am UTC](https://forums.developer.nvidia.com/t/when-should-cudathreadsynchronize-be-called/19357/6 "2010-10-22T09:40:33Z")

</div>

> [@](#):
>
> The first two are redundant, as kernels in the same stream are launched sequentially anyway, and no code is executed on the CPU between the kernels.
> 
> The third call does serve a purpose, without it the “Done” message would appear before execution is actually completed.

Thanks. Very helpful.
