|
|
|
|
|
|
|
|
|
|
|
|
#include <process.h> |
|
|
|
スレッド生成 |
|
Dim thread As New Thread(New
ThreadStart(AddressOf スレッドルーチン)) |
int rc;
pthread_attr_t attr;
rc=pthread_attr_init(&attr);
if(rc!=0) fprintf(stderr,"属性初期化 error");
rc=pthread_attr_setstacksize(&attr, 2*1024*1024);
if(rc!=0) fprintf(stderr,"スタックサイズ error"); |
|
|
Thread thread=new スレッドクラス(); |
Thread thread=new Thread(new
ThreadStart(スレッド関数)); |
スレッド開始 |
|
thread.Start() |
pthread_t tid;
rc=pthread_create(&tid, &attr, スレッド関数, NULL);
if(rc!=0) fprintf(stderr,"スレッド開始 error"); |
uintptr_t tid = _beginthread(スレッド関数,
0, NULL);
if(tid == -1L) perror("beginthread error"); |
AfxBeginThread(スレッド関数, NULL); |
thread.start(); |
thread.Start(); |
終了待ち |
|
|
|
|
|
thread.isAlive() |
thread.IsAlive |
|
thread.Join() |
rc=pthread_join(tid, NULL);
if(rc!=0) fprintf(stderr,"スレッド合流 error"); |
if(WaitForSingleObject((HANDLE)tid,
INFINITE)==WAIT_FAILED)
fprintf(stderr, "wait error:%d\n", GetLastError()); |
|
thread.join(); |
thread.Join(); |
始末 |
|
|
rc=pthread_attr_destroy(&attr);
if(rc!=0) fprintf(stderr,"属性破棄 error"); |
|
|
|
|
スレッド本体 |
|
Sub スレッドルーチン()
Do While 条件
〜
Loop
End Sub |
void* スレッド関数(void *arg)
{
while(条件){
〜
}
return NULL;
} |
void __cdecl スレッド関数(void
*arg)
{
while(条件){
〜
}
_endthread();
} |
UINT AFX_CDECL スレッド関数(LPVOID
pParam)
{
while(条件){
〜
}
return 0;
} |
class スレッドクラス extends
Thread {
public void run() {
while(条件){
〜
}
}
} |
void スレッド関数()
{
while(条件)
{
〜
}
} |
|
|
pthread_exit(NULL); |
|
AfxEndThread(0); |
|
|
自分 |
|
|
pthread_t tid=pthread_self(); |
HANDLE h=GetCurrentThread(); |
CWinThread *p=AfxGetThread(); |
Thread t=Thread.currentThread(); |
Thread thread = Thread.CurrentThread; |
スリープ |
WScript.sleep ミリ秒 |
Thread.Sleep(ミリ秒) |
sleep(秒);
usleep(マイクロ秒); usleep()は古いのでnanosleep()が推奨らしい
struct timespec ts={秒, ナノ秒};
int rc=nanosleep(&ts, NULL);
if(rc<0 && errno==EINTR){
//割り込まれた
}
↓selectの本来の使い方とは違うので間違いと言われる方法
struct timeval tv={秒, マイクロ秒};
int rc=select(0, NULL, NULL, NULL, &tv);
if(rc<0 && errno==EINTR){
//割り込まれた
} |
Sleep(ミリ秒); |
::Sleep(ミリ秒); |
try{
Thread.sleep(ミリ秒);
}catch(InterruptedException e){
//割り込まれた
} |
Thread.Sleep(ミリ秒); |
|
|
|
|
|
|
|
|
クリティカルセクション |
|
Dim lk As New Object 'スレッド共有 |
pthread_mutex_t lk; /*スレッド共有*/
pthread_mutex_init(&lk, NULL); |
CRITICAL_SECTION mutex; //スレッド共有
InitializeCriticalSection(&mutex); |
CCriticalSection mutex; //スレッド共有 |
Object lk=new Object(); //スレッド共有 |
Object lk=new Object(); //スレッド共有 |
|
|
rc=pthread_mutex_trylock(&lk);
if(rc==EBUSY) fprintf(stderr,"ロック中");
else if(rc!=0) fprintf(stderr,"ミューテックスロック試行 error"); |
if(!TryEnterCriticalSection(mutex))
printf("ロック中"\n");
.NETのみ使用可能 |
CSingleLock lk(&mutex); //スレッドのローカル変数
if(lk.IsLocked()) TRACE0("ロック中"); |
|
|
|
SyncLock lk |
rc=pthread_mutex_lock(&lk);
if(rc!=0) fprintf(stderr,"ミューテックスロック error"); |
EnterCriticalSection(&mutex); |
lk.Lock(); |
synchronized(lk){ |
lock (lk)
{ |
|
End SyncLock |
rc=pthread_mutex_unlock(&lk);
if(rc!=0) fprintf(stderr,"ミューテックスアンロック error"); |
LeaveCriticalSection(&mutex); |
lk.Unlock(); |
} |
} |
|
|
rc=pthread_mutex_destroy(&lk);
if(rc!=0) fprintf(stderr,"ミューテックス破棄 error"); |
DeleteCriticalSection(&mutex); |
|
|
|
セマフォ |
|
|
sem_t obj;
sem_t *sem=&obj;
rc=sem_init(sem, 0, 1);
if(rc!=0) perror("セマフォ初期化"); |
↓プロセス間セマフォ新規
rc=sem_unlink("/セマフォ名");
if(rc!=0){
if(errno==ENOENT){
/*No such file or directory*/
}else perror("セマフォ削除");
}
sem_t *sem=sem_open("/セマフォ名", O_CREAT, 0600, 1);
if(sem==SEM_FAILED) perror("セマフォ作成");
↓プロセス間セマフォ既存
sem_t *sem=sem_open("/セマフォ名", 0);
if(sem==SEM_FAILED) perror("セマフォオープン");
なお、セマフォの実体は「/tmp/.セマフォ名」に在る |
HANDLE sem = CreateSemaphore(NULL, 1, 1,
NULL);
if(sem==NULL)
fprintf(stderr, "CreateSemaphore error:%d\n", GetLastError()); |
CSemaphore sem; |
Semaphore sem = new
Semaphore(1); |
|
|
|
int val;
rc=sem_getvalue(sem,&val);
if(rc!=0) perror("セマフォ取得"); |
|
|
int val = sem.drainPermits(); |
|
|
|
rc=sem_trywait(sem);
if(rc!=0){
if(rc==EAGAIN) fprintf(stderr,"ロック中");
else perror("セマフォロック試行");
} |
|
CSingleLock lk(&sem); //スレッドのローカル変数
if(lk.IsLocked()) TRACE0("ロック中"); |
if (!sem.tryAcquire())
System.out.println("ロック中"); |
|
|
|
rc=sem_wait(sem);
if(rc!=0) perror("セマフォロック"); |
if(WaitForSingleObject(sem,
INFINITE)==WAIT_FAILED)
fprintf(stderr, "sem_wait error:%d\n", GetLastError()); |
lk.Lock(); |
sem.acquire(); |
|
|
|
rc=sem_post(sem);
if(rc!=0) perror("セマフォアンロック"); |
if(!ReleaseSemaphore(sem, 1, NULL))
fprintf(stderr, "sem_post error:%d\n", GetLastError()); |
lk.Unlock(); |
sem.release(); |
|
|
|
rc=sem_destroy(sem);
if(rc!=0) perror("セマフォ破棄"); |
rc=sem_close(sem);
if(rc!=0) perror("セマフォクローズ"); |
if(!CloseHandle(sem))
fprintf(stderr, "sem_term error:%d\n", GetLastError()); |
|
|
|
読み書きロック |
|
lk As New ReaderWriterLock() 'スレッド共有 |
pthread_rwlock_t lk; /*スレッド共有*/
rc=pthread_rwlock_init(&lk, NULL);
if(rc!=0) fprintf(stderr,"読み書きロック初期化 error"); |
|
|
ReadWriteLock lk = new
ReentrantReadWriteLock(); |
ReaderWriterLock lk = new ReaderWriterLock();
//スレッド共有 |
|
Try
lk.AcquireReaderLock(1) '読み
lk.AcquireWriterLock(1) '書き
Catch e As ApplicationException
'ロック中 |
rc=pthread_rwlock_tryrdlock(&lk);
//読み
rc=pthread_rwlock_trywrlock(&lk); //書き
if(rc==EBUSY) fprintf(stderr,"ロック中");
else if(rc!=0) fprintf(stderr,"読み書きロック試行 error"); |
|
|
if (!lk.readLock().tryLock()) {
//ロック中
}
if (!lk.writeLock().tryLock()) {
//ロック中
} |
try
{
lk.AcquireReaderLock(1); //読み
lk.AcquireWriterLock(1); //書き
}
catch (ApplicationException
)
{
//ロック中
} |
|
Try
lk.AcquireReaderLock(Timeout.Infinite) '読み
lk.AcquireWriterLock(Timeout.Infinite) '書き |
rc=pthread_rwlock_rdlock(&lk);
//読み
rc=pthread_rwlock_wrlock(&lk); //書き
if(rc!=0) fprintf(stderr,"読み書きロック error"); |
|
|
try {
lk.readLock().lock(); //読み
lk.writeLock().lock(); //書き |
try
{
lk.AcquireReaderLock(Timeout.Infinite); //読み
lk.AcquireWriterLock(Timeout.Infinite); //書き |
|
Finally
lk.ReleaseReaderLock() '読み
lk.ReleaseWriterLock() '書き
End Try |
rc=pthread_rwlock_unlock(&lk);
//読み書き
if(rc!=0) fprintf(stderr,"読み書きアンロック error"); |
|
|
} finally {
lk.readLock().unlock(); //読み
lk.writeLock().unlock(); //書き
} |
}
finally
{
lk.ReleaseReaderLock(); //読み
lk.ReleaseWriterLock(); //書き
} |
|
|
rc=pthread_rwlock_destroy(&lk);
if(rc!=0) fprintf(stderr,"読み書きロック破棄 error"); |
|
|
|
|
|
|
|
|
|
|
|
|
カウンター |
|
|
|
|
|
AtomicBoolean
AtomicInteger
AtomicLong |
|