Multithreaded noob question
Posted: Fri Sep 06, 2019 6:54 am
In the following code is my understanding so far. My multithreaded model is different. I just want to experiment with the idea. The main thread will do no searching. It will only be a servicing thread that will monitor the search threads. It will communicate with the search threads by polling a global data structure.
In main() StartSearch() is called. In StartSearch() a thread structure is created with new creating the pointer t. The structure is initialized. Then the thread is created, "std::thread t1(SearchControl);" SearchControl() will be the top level search thread that will create other search threads.
I have two questions.
1. Will this work as I have it?
2. How do I pass t to SearchControl?
I know there are other things I have to learn like how to stop and free threads and who knows what else. But for now I just need to know if I am on the right path. Thanks.
In main() StartSearch() is called. In StartSearch() a thread structure is created with new creating the pointer t. The structure is initialized. Then the thread is created, "std::thread t1(SearchControl);" SearchControl() will be the top level search thread that will create other search threads.
I have two questions.
1. Will this work as I have it?
2. How do I pass t to SearchControl?
I know there are other things I have to learn like how to stop and free threads and who knows what else. But for now I just need to know if I am on the right path. Thanks.
Code: Select all
// Honeybee.cpp
#include <thread>
using namespace std;
#define s08 signed char
#define u08 unsigned char
#define s32 signed long
#define u32 unsigned long
#define u64 unsigned long long
enum {EXIT, IDLE, SEARCH};
enum {EDGE = -1, NONE, WP, WN, WB, WR, WQ, WK, BP, BN, BB, BR, BQ, BK};
typedef struct {
u32 base;
u32 ply;
u64 wPieces;
u64 bPieces;
u08 board[120];
u08 fs[1000];
u08 ts[1000];
u08 cp[1000];
} threadS;
threadS game;
u08 state;
s08 iBoard[] = { EDGE, EDGE, EDGE, EDGE, EDGE, EDGE, EDGE, EDGE, EDGE, EDGE,
EDGE, EDGE, EDGE, EDGE, EDGE, EDGE, EDGE, EDGE, EDGE, EDGE,
EDGE, WR, WN, WB, WQ, WK, WB, WN, WR, EDGE,
EDGE, WP, WP, WP, WP, WP, WP, WP, WP, EDGE,
EDGE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, EDGE,
EDGE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, EDGE,
EDGE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, EDGE,
EDGE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, EDGE,
EDGE, BP, BP, BP, BP, BP, BP, BP, BP, EDGE,
EDGE, BR, BN, BB, BQ, BK, BB, BN, BR, EDGE,
EDGE, EDGE, EDGE, EDGE, EDGE, EDGE, EDGE, EDGE, EDGE, EDGE,
EDGE, EDGE, EDGE, EDGE, EDGE, EDGE, EDGE, EDGE, EDGE, EDGE };
void SearchControl(threadS* t) {
}
void StartSearch() {
threadS* t = new(threadS);
// code to intialise *t here
std::thread t1(SearchControl);
}
void GetCmd() {
}
void NewGame() {
u08 sq;
game.base = 0;
game.ply = 0;
for (sq == 0; sq <= 120; sq++) {
game.board[sq] = iBoard[sq];
}
}
void Initialize() {
NewGame();
}
s32 main() {
state = IDLE;
Initialize();
while (state) {
if (state == SEARCH) StartSearch();
GetCmd();
}
return state;
}