Created
February 19, 2017 11:10
-
-
Save baoilleach/9416aa0572e350531327cb3d05e2ff17 to your computer and use it in GitHub Desktop.
Test multithreading with a simple example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <process.h> | |
#ifdef WIN32 | |
#define USING_OBDLL | |
#endif | |
#include <openbabel/babelconfig.h> | |
#include <openbabel/obconversion.h> | |
#include <openbabel/mol.h> | |
#include <openbabel/parsmart.h> | |
#define MAX_THREADS 4 | |
HANDLE hScreenMutex; // "Screen update" mutex | |
int ThreadNr; // Number of threads started | |
using namespace OpenBabel; | |
struct ThreadData { | |
ThreadData(OBSmartsPattern* _pat, OBMol* _mol) : pat(_pat), mol(_mol) {}; | |
OBSmartsPattern* pat; | |
OBMol* mol; | |
}; | |
void DoSearch(void* threadData) | |
{ | |
ThreadData* td = (ThreadData*)threadData; | |
OBSmartsPattern* pat = td->pat; | |
OBMol* mol = td->mol; | |
std::vector<std::vector<int> > mlist; | |
pat->Match(*mol, mlist); | |
//WaitForSingleObject(hScreenMutex, INFINITE); | |
//printf("Finishing thread %d\n", ThreadNr); | |
//ReleaseMutex(hScreenMutex); | |
InterlockedDecrement((LONG*)&ThreadNr); | |
} | |
void StartThreads(void) // Dispatch and count threads. | |
{ | |
obErrorLog.StopLogging(); | |
OBConversion conv; | |
conv.SetInFormat("smi"); | |
OBMol *molA = new OBMol(); | |
OBMol *molB = new OBMol(); | |
conv.ReadString(molA, "C"); | |
conv.ReadString(molB, "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); | |
OBSmartsPattern *patA = new OBSmartsPattern(); | |
patA->Init("[#6]"); | |
ThreadData *tdA = new ThreadData(patA, molA); | |
ThreadData *tdB = new ThreadData(patA, molB); | |
ThreadNr = 2; | |
_beginthread(DoSearch, 0, tdA); | |
_beginthread(DoSearch, 0, tdB); | |
while (ThreadNr > 0) { | |
Sleep(250); | |
} | |
} | |
int main() // Thread One | |
{ | |
// Create the mutexes and reset thread count. | |
hScreenMutex = CreateMutex(NULL, FALSE, NULL); // Cleared | |
ThreadNr = 0; | |
StartThreads(); | |
// All threads done. Clean up handles. | |
CloseHandle(hScreenMutex); | |
getchar(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment