CUDA context and multi-threading

I try to use a cuda context in differents CPU threads. I think it is possible to do it, like in threadMigration example from the cuda SDK.

To do so, I use the following method :

  • host thread : cuCtxCreate(&context)… cuCtxPopCurrent()
  • thread 1 : cuCtxPushCurrent(context) … CUDA job … cuCtxSynchronize(); cuCtxPopCurrent()
  • host thread wait for thread1, then launch thread 2
  • thread 2 : cuCtxPushCurrent(context) … CUDA job … cuCtxSynchronize(); cuCtxPopCurrent()

For each call, I verify the return values. And the program abort on the second (thread 2) cuCtxPushCurrent(context), with an CUDA_ERROR_INVALID_VALUE.
I also verified the value of the context pointer, and it’s always the same.

Does someone have an idea of why this method works only for the first thread ?

Thanks !