gcc (GCC) 4.1.2
c89
你好,鎖定和解鎖多線程應用程序
Decideing,我需要做的在多線程應用程序鎖定和解鎖。
保持代碼片段的簡短。我有一個全球渠道結構。即
typedef struct tag_channel channel_t;
struct tag_channel {....};
我有3個函數使用API來設置和處理其消息隊列上的通道。
我的主線程#1將調用這個函數
apr_status_t set_ss7_channel_state(channel_t *channel)
{
/* API call to set channel - non-blocking ASYNC call that returns immediately
wait for event in evt_loop */
setChanState(channel);
}
事件循環中產生的線程#2開始。其他功能可以觸發相同的頻道購買將頻道放在消息隊列中。
static void* APR_THREAD_FUNC evt_loop(apr_thread_t *thd, void *data)
{
while(is_looping) {
/* Get event and channel from message API message queue */
waitevt();
if(channel_process(channel) != TRUE) {
/* clean up */
}
}
}
從線程#稱爲Process通道2
apr_status_t channel_process(channel)
{
/* process channel here based on the event
/* lock channel */
/* do some processing */
/* unlock channel */
}
所以基本上調用工作這樣一個單信道:
1) setChanState(channel) thread #1 -> puts channel on an API message queue
2) evt_loop(...) thread #2 will retrieve the event and the channel structure
3) process_channel(channel) will process the channel on thread #2
我在想,我需要阻止渠道結構,因爲這個渠道可能會有其他活動? 我已經在channel_process上放置了阻塞。
非常感謝您的任何建議,
我不明白你打算如何在這裏使用多個線程。此外,您的問題不可能像現在這樣回答,因爲「此通道上的其他事件」不是簡單地發生,而是其他代碼執行某些會影響通道的代碼的結果。不知道更多,這是不可能回答。 –