- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 threads,each will call "activateFD" and "deactiveFD" respectively to make a socket into active/deactive status.any protential risk?
if reorder doesn't happen (add memory barrier before atomic_set),everything will be ok?
i am afraid that 2 threads will call atomic_set on a same variable? and what bad result will be incurred?
void deActivateFD()
{
uint32_t flg = (uint32_t) atomic_read( &fdActive );
if(flg == 0)
return;
if( emptySock() ) //read all buf from sock
atomic_set(&fdActive,0);
}
void activateFD()
{
uint32_t flg = (uint32_t) atomic_read( &fdActive );
if(flg == 1)
return;
bool tmpFdActive = fillSock()//write something to socket
if(tmpFdActive)
atomic_set(&fdActive,1);
}
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Here are my 2 cents,
1) If you are relying/want your program (the two functions in this case) to execute in a particular order, I am afraid there won't be much (any?) parallelism in that case.
2) Since you are using the atomic_set() call, you probably need not worry about anything "bad" happening w.r.t reading/writing to the same variable. The following link (http://qnxcs.unomaha.edu/help/product/neutrino/lib_ref/a/atomic_set.html) that I found when I did a Google search on "atomic_set" tells me that it (atomic_set) is a thread safe way of setting the bits of a variable (you seem to be doing exactly this in your two functions).
Please let me know if this isn't clear. Please also provide more information/context about the problem you are facing if possible. I will be happy to help.
Thank You,
Krishna
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I want to sync between 2 threads but I don't want to use any lock which will bring latency. so I use these code:
if (atomic_read(var) == 1)
{
send(sockfd,buf,size);
atomic_set(var,0);
}
//
if (atomic_read(var) == 0)
{
read(sockfd,buf,size);
atomic_set(var,1);
}
I am not sure this kind of method will bring any protential risk.
i.e.
1.is it a correct sync method? will read & send sockfd at same time?
2.as atomic_set is not atomic operation on X86 SMP actually,var might be a undefine value.
PS:
some one said atomic_set() is not atomic operation actually on linux X86 SMP platform.
static inline void atomic_set(atomic_t *v, int i)
36 {
37 v->counter = i;
38 }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Meaning only one thread performs the writes while the other thread performs the read.
Where write complete (message ready) and read complete (message read) is controlled by the single shared variable.
For two way communication you would extend the method to include an additional flag.
As an alternate technique, consider using a pointer to message in lieu of a flag and a single message buffer.
Message ready is indicated by writing message pointer over (NULL) communication variable.
Message taken (but not necessarily finished reading) is indicated by writing NULL over communication variable
bool Send(void* msg)
{
if(pMsg) return false;
pMsg = msg;
return true;
}
void* Get()
{
void* msg = pMsg;
if(msg) pMsg = NULL; // NULLify only when message taken
return msg;
}
With this technique you can have literal messages and provide a degree of double buffering.
Note, this still is one way communication without use of interlocked instructions.
add atomic_read and atomic_write if you want
and/or make pMsg volatile.
Jim Dempsey
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page